package com.haikesi.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.haikesi.api.base.*;
import com.haikesi.api.base.Error;
import com.haikesi.exception.BusinessException;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName ButlerApiUtils 大管家相关接口
 * @Author HandsomeBugs
 * @Date 2019/5/15 14:28
 * @Version 1.0
 **/
@Component
public class ButlerApiUtils {

    private static Log log = LogFactory.getLog(ButlerApiUtils.class);
//    private static String serviceUrl = "http://www.beoneaut.com:6888/dataService/doService";
//    private static String appid = "hks";
//    private static String appkey = "ACD755B13CB341EFB5B860EC120541972D66F61AB0D33552A98A1D6518D9DCAA";
//    private static String ap_id = "10123";//AP010123


    private static RedisUtils redisUtils;
    @Autowired
    public ButlerApiUtils(RedisUtils redis) {
        ButlerApiUtils.redisUtils = redis;
    }

    public static String getToken() {
        String serviceUrl= redisUtils.get(Constants.BIG_SERVICE_URL);
        String appid = redisUtils.get(Constants.BIG_APP_ID);
        String appkey = redisUtils.get(Constants.BIG_APP_KEY);
        String ap_id = redisUtils.get(Constants.BIG_AP_ID);

        String url = serviceUrl;//请求 url
        JSONObject paramData = new JSONObject();
        paramData.put("appid", appid);
        paramData.put("appkey", appkey);
        paramData.put("appcode", "V0001");
        paramData.put("acctoken", "");
        paramData.put("processTime", DateUtils.getDate("yyyyMMddHHmmss"));
        log.info("===========请求URl:" + url);
        log.info("===========请求参数:" + "$reqdata=" + paramData.toString());
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/x-www-form-urlencoded");
        ResponseEntity<JSONObject> response = RestTemplateUtils.post(url, headers, "$reqdata=" + paramData.toString(), JSONObject.class);
        log.info("===========ResponseEntity:" + response);
        JSONObject result = response.getBody();
        if ("0000".equals(result.get("resultCode").toString())) {
            // JSONObject resultContent=(JSONObject) result.get("resultContent");
            HashMap<String, String> resultContent = (HashMap<String, String>) result.get("resultContent");
            String acctoken = (String) resultContent.get("acctoken");
            //String expiredTime = (String) resultContent.get("expiredTime");
            return acctoken;
        }
        return "";
    }


    /**
     * 添加账号
     *
     * @param password
     * @param user_name
     * @param user_phone
     * @param id_no
     * @param gender
     * @param email
     * @param area_code1
     * @param area_code2
     * @param area_code3
     * @return
     */
    public static String createAccount(String password, String user_name, String user_phone, String id_no, String gender, String email, String area_code1, String area_code2, String area_code3) {

        String serviceUrl= redisUtils.get(Constants.BIG_SERVICE_URL);
        String appid = redisUtils.get(Constants.BIG_APP_ID);
        String appkey = redisUtils.get(Constants.BIG_APP_KEY);
        String ap_id = redisUtils.get(Constants.BIG_AP_ID);

        String url = serviceUrl;//请求 url

        JSONObject paramData = new JSONObject();
        paramData.put("appid", appid);
        paramData.put("appkey", appkey);
        paramData.put("appcode", "V0005");
        paramData.put("acctoken", getToken());
        if (StringUtils.isBlank(paramData.get("acctoken") + "")) {
            return Strings.EMPTY;
        }

        paramData.put("processTime", DateUtils.getDate("yyyyMMddHHmmss"));
        JSONObject serviceContent = new JSONObject();
        serviceContent.put("password", password);
        serviceContent.put("user_name", user_name);
        serviceContent.put("user_phone", user_phone);
        serviceContent.put("id_no", id_no);
        serviceContent.put("gender", gender);
        serviceContent.put("email", email);
        serviceContent.put("ap_id", ap_id);
        serviceContent.put("area_code1", area_code1);
        serviceContent.put("area_code2", area_code2);
        serviceContent.put("area_code3", area_code3);
        paramData.put("serviceContent", serviceContent);
        log.info("===========请求URl:" + url);
        log.info("===========请求参数:" + "$reqdata=" + paramData.toString());
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/x-www-form-urlencoded");
        ResponseEntity<JSONObject> response = RestTemplateUtils.post(url, headers, "$reqdata=" + paramData.toString(), JSONObject.class);
        log.info("===========ResponseEntity:" + response);
        JSONObject result = response.getBody();
        if ("0000".equals(result.get("resultCode").toString())) {
            HashMap<String, String> resultContent = (HashMap<String, String>) result.get("resultContent");
            String account = (String) resultContent.get("account");
            //  JSONObject resultContent=(JSONObject) result.get("resultContent");
            return account;
        }
        return Strings.EMPTY;
    }

    /**
     * 删除账号
     *
     * @param account
     * @return
     */
    public static String deleteAccount(String account) {
        String serviceUrl= redisUtils.get(Constants.BIG_SERVICE_URL);
        String appid = redisUtils.get(Constants.BIG_APP_ID);
        String appkey = redisUtils.get(Constants.BIG_APP_KEY);
        String ap_id = redisUtils.get(Constants.BIG_AP_ID);


        String url = serviceUrl;//请求 url

        JSONObject paramData = new JSONObject();
        paramData.put("appid", appid);
        paramData.put("appkey", appkey);
        paramData.put("appcode", "V0006");
        paramData.put("acctoken", getToken());
        if (StringUtils.isBlank(paramData.get("acctoken") + "")) {
            return Strings.EMPTY;
        }
        paramData.put("processTime", DateUtils.getDate("yyyyMMddHHmmss"));
        JSONObject serviceContent = new JSONObject();
        serviceContent.put("account", account);
        paramData.put("serviceContent", serviceContent);
        log.info("===========请求URl:" + url);
        log.info("===========请求参数:" + "$reqdata=" + paramData.toString());
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/x-www-form-urlencoded");
        ResponseEntity<JSONObject> response = RestTemplateUtils.post(url, headers, "$reqdata=" + paramData.toString(), JSONObject.class);
        log.info("===========ResponseEntity:" + response);
        JSONObject result = response.getBody();
        if ("0000".equals(result.get("resultCode").toString())) {
            return result.get("resultCode").toString();
        }
        return Strings.EMPTY;
    }


    /**
     * 修加账号
     *
     * @param password
     * @param account
     * @param user_name
     * @param user_phone
     * @param id_no
     * @param gender
     * @param email
     * @param area_code1
     * @param area_code2
     * @param area_code3
     * @return
     */
    public static String updateAccount(String account, String password, String user_name, String user_phone, String id_no, String gender, String email, String area_code1, String area_code2, String area_code3) {
        String serviceUrl= redisUtils.get(Constants.BIG_SERVICE_URL);
        String appid = redisUtils.get(Constants.BIG_APP_ID);
        String appkey = redisUtils.get(Constants.BIG_APP_KEY);
        String ap_id = redisUtils.get(Constants.BIG_AP_ID);


        String url = serviceUrl;//请求 url
        JSONObject paramData = new JSONObject();
        paramData.put("appid", appid);
        paramData.put("appkey", appkey);
        paramData.put("appcode", "V0007");
        paramData.put("acctoken", getToken());
        if (StringUtils.isBlank(paramData.get("acctoken") + "")) {
            return Strings.EMPTY;
        }
        paramData.put("processTime", DateUtils.getDate("yyyyMMddHHmmss"));
        JSONObject serviceContent = new JSONObject();
        serviceContent.put("account", account);
        serviceContent.put("password", password);
        serviceContent.put("user_name", user_name);
        serviceContent.put("user_phone", user_phone);
        serviceContent.put("id_no", id_no);
        serviceContent.put("gender", gender);
        serviceContent.put("email", email);
        serviceContent.put("ap_id", ap_id);
        serviceContent.put("area_code1", area_code1);
        serviceContent.put("area_code2", area_code2);
        serviceContent.put("area_code3", area_code3);
        paramData.put("serviceContent", serviceContent);
        log.info("===========请求URl:" + url);
        log.info("===========请求参数:" + "$reqdata=" + paramData.toString());
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/x-www-form-urlencoded");
        ResponseEntity<JSONObject> response = RestTemplateUtils.post(url, headers, "$reqdata=" + paramData.toString(), JSONObject.class);
        log.info("===========ResponseEntity:" + response);
        JSONObject result = response.getBody();
        if ("0000".equals(result.get("resultCode").toString())) {
            return result.get("resultCode").toString();
        }
        return Strings.EMPTY;
    }

    /**
     * 绑定账号
     *
     * @param account
     * @param serial_num
     * @param genuine_code
     * @return
     */
    public static String bindDevice(String account, String serial_num, String genuine_code) {

        String serviceUrl= redisUtils.get(Constants.BIG_SERVICE_URL);
        String appid = redisUtils.get(Constants.BIG_APP_ID);
        String appkey = redisUtils.get(Constants.BIG_APP_KEY);
        String ap_id = redisUtils.get(Constants.BIG_AP_ID);

        String url = serviceUrl;//请求 url

        JSONObject paramData = new JSONObject();
        paramData.put("appid", appid);
        paramData.put("appkey", appkey);
        paramData.put("appcode", "V0012");
        paramData.put("acctoken", getToken());
        if (StringUtils.isBlank(paramData.get("acctoken") + "")) {
            return Strings.EMPTY;
        }
        paramData.put("processTime", DateUtils.getDate("yyyyMMddHHmmss"));
        JSONObject serviceContent = new JSONObject();
        serviceContent.put("account", account);
        serviceContent.put("serial_num", serial_num);
        serviceContent.put("genuine_code", genuine_code);
        paramData.put("serviceContent", serviceContent);
        log.info("===========请求URl:" + url);
        log.info("===========请求参数:" + "$reqdata=" + paramData.toString());
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/x-www-form-urlencoded");
        ResponseEntity<JSONObject> response = RestTemplateUtils.post(url, headers, "$reqdata=" + paramData.toString(), JSONObject.class);
        log.info("===========ResponseEntity:" + response);
        JSONObject result = response.getBody();
        if ("0000".equals(result.get("resultCode").toString())) {
            return result.get("resultContent").toString();
        } else {
            throw new BusinessException(Type.EXIST_ERROR, ErrorTools.ErrorAsArrayList(new Error("bindDevice", result.get("resultMsg").toString())));
        }
    }

    /**
     * 解除绑定账号
     *
     * @param account
     * @param serial_num
     * @return
     */
    public static String unbindDevice(String account, String serial_num) {
        String serviceUrl= redisUtils.get(Constants.BIG_SERVICE_URL);
        String appid = redisUtils.get(Constants.BIG_APP_ID);
        String appkey = redisUtils.get(Constants.BIG_APP_KEY);
        String ap_id = redisUtils.get(Constants.BIG_AP_ID);


        String url = serviceUrl;//请求 url

        JSONObject paramData = new JSONObject();
        paramData.put("appid", appid);
        paramData.put("appkey", appkey);
        paramData.put("appcode", "V0013");
        paramData.put("acctoken", getToken());
        if (StringUtils.isBlank(paramData.get("acctoken") + "")) {
            return Strings.EMPTY;
        }

        paramData.put("processTime", DateUtils.getDate("yyyyMMddHHmmss"));
        JSONObject serviceContent = new JSONObject();
        serviceContent.put("account", account);
        serviceContent.put("serial_num", serial_num);
        paramData.put("serviceContent", serviceContent);
        log.info("===========请求URl:" + url);
        log.info("===========请求参数:" + "$reqdata=" + paramData.toString());
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/x-www-form-urlencoded");
        ResponseEntity<JSONObject> response = RestTemplateUtils.post(url, headers, "$reqdata=" + paramData.toString(), JSONObject.class);
        log.info("===========ResponseEntity:" + response);
        JSONObject result = response.getBody();
        if ("0000".equals(result.get("resultCode").toString())) {
            return result.get("resultCode").toString();
        } else {
            throw new BusinessException(Type.EXIST_ERROR, ErrorTools.ErrorAsArrayList(new Error("bindDevice", result.get("resultMsg").toString())));
        }
    }


    /**
     * 查询执行计划进程设备的条件类型
     *
     * @param productCode 设备标识码
     * @return
     */
    public static String queryPlanDriverCondition(String productCode) {
        String serviceUrl= redisUtils.get(Constants.BIG_SERVICE_URL);
        String appid = redisUtils.get(Constants.BIG_APP_ID);
        String appkey = redisUtils.get(Constants.BIG_APP_KEY);
        String ap_id = redisUtils.get(Constants.BIG_AP_ID);

        String url = serviceUrl;//请求 url

        JSONObject paramData1 = new JSONObject();
        paramData1.put("productCode", productCode);

        JSONObject paramData = new JSONObject();
        paramData.put("appid", appid);
        paramData.put("appkey", appkey);
        paramData.put("appcode", "V0019");
        paramData.put("acctoken", getToken());
        if (StringUtils.isBlank(paramData.get("acctoken") + "")) {
            return Strings.EMPTY;
        }
        paramData.put("processTime", DateUtils.getDate("yyyyMMddHHmmss"));
        paramData.put("serviceContent", paramData1);

        log.info("===========请求URl:" + url);
        log.info("===========请求参数:" + "$reqdata=" + paramData.toString());
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/x-www-form-urlencoded");
        ResponseEntity<JSONObject> response = RestTemplateUtils.post(url, headers, "$reqdata=" + paramData.toString(), JSONObject.class);
        log.info("===========ResponseEntity:" + response);
        JSONObject result = response.getBody();
        if ("0000".equals(result.get("resultCode").toString())) {
            JSONArray jsonArray = result.getJSONArray("resultContent");
            return JSON.toJSONString(jsonArray);
        } else {
            throw new BusinessException(Type.EXCEPTION_FAIL, ErrorTools.ErrorAsArrayList(new Error("bindDevice", result.get("resultMsg").toString())));
        }
    }
}
